package cn.edu.zut.cs.controller;


import cn.edu.zut.cs.pojo.Equipment;
import cn.edu.zut.cs.pojo.User;
import cn.edu.zut.cs.pojo.Weighing;
import cn.edu.zut.cs.pojo.WeighingVO;
import cn.edu.zut.cs.service.EquipmentService;
import cn.edu.zut.cs.service.WeighingService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.ibatis.jdbc.Null;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.ui.Model;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;
import org.springframework.web.context.request.WebRequest;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

/**
 * <p>
 * 称重数据管理 前端控制器
 * </p>
 *
 * @author weighing
 * @since 2021-06-16
 */
@Controller
public class WeighingController {

    @Resource
    private WeighingService weighingService;

    @Resource
    private EquipmentService equipmentService;


//    @RequestMapping("/weightinglist")
//    public String selectOneweightdata(HttpSession session,@RequestParam(value ="weighttime1",required = false)String date1, @RequestParam(value ="weighttime2",required = false)String date2, @RequestParam(value = "pn",defaultValue = "1")Integer pn, Model model){
//        User loginUser = (User) session.getAttribute("loginUser");
//        if(date1==null||date2==null){
//            List<Weighing> weigihings = weighingService.selectAllWeighting(loginUser);
//            Page<Weighing> Tpage = new Page<>(pn,10);
//            Page<Weighing> page = weighingService.page(Tpage, null);
//            List<Weighing> records = page.getRecords();
//            model.addAttribute("weighings",page);
//            model.addAttribute("weightstart",date1);
//            model.addAttribute("weightend",date2);
//            return "weight_data";
//        }else{
//            Page<Weighing> Tpage = new Page<>(pn,10);
//            QueryWrapper<Weighing> queryWrapper = new QueryWrapper<>();
//            queryWrapper.ge("weighttime",date1).le("weighttime",date2).eq("weicompany",loginUser.getUsercompany()).eq("weilocation",loginUser.getUserlocation());
//            Page<Weighing> page = weighingService.page(Tpage, queryWrapper);
//            model.addAttribute("weighings",page);
//            model.addAttribute("weightstart",date1);
//            model.addAttribute("weightend",date2);
//            return "weight_data1";
//        }
//
//    }

    //查询某段时间内的所有称重记录（权限划分）
    @RequestMapping("/weightinglist/sea")
    public String selectOneweightdata1(HttpSession session,@RequestParam(value ="weighttime1",required = false,defaultValue = "2001-06-19 16:16:12")String date1, @RequestParam(value ="weighttime2",required = false,defaultValue = "4000-06-19 16:16:12")String date2, @RequestParam(value = "weilocation",defaultValue = "-1")String weilocation, @RequestParam(value = "pn",defaultValue = "1")Integer pn, Model model){
        User loginUser = (User) session.getAttribute("loginUser");
        Page<Weighing> Tpage = new Page<>(pn,10);
        QueryWrapper<Weighing> queryWrapper = new QueryWrapper<>();
        if(!date1.equals("2001-06-19 16:16:12")&&!date2.equals("4000-06-19 16:16:12")&&weilocation.equals("-1")){
            if(loginUser.getUserlevel()==1){
                queryWrapper.ge("weighttime",date1)
                            .le("weighttime",date2)
                            .eq("weicompany",loginUser.getUsercompany());
            }else{
                queryWrapper.ge("weighttime",date1)
                            .le("weighttime",date2)
                            .eq("weicompany",loginUser.getUsercompany())
                            .eq("weilocation",loginUser.getUserlocation());
            }
            Page<Weighing> page = weighingService.page(Tpage, queryWrapper);
            model.addAttribute("weighings",page);
            model.addAttribute("weightstart",date1);
            model.addAttribute("weightend",date2);
            model.addAttribute("weilocation",weilocation);
            return "weight_data1";
        }else if(!date1.equals("2001-06-19 16:16:12")&&!date2.equals("4000-06-19 16:16:12")&&!weilocation.equals("-1")){
            if(loginUser.getUserlevel()==1){
                queryWrapper.ge("weighttime",date1)
                        .le("weighttime",date2)
                        .eq("weicompany",loginUser.getUsercompany())
                        .eq("weilocation",weilocation);
            }else{
                queryWrapper.ge("weighttime",date1)
                        .le("weighttime",date2)
                        .eq("weicompany",loginUser.getUsercompany())
                        .eq("weilocation",loginUser.getUserlocation());
            }
            Page<Weighing> page = weighingService.page(Tpage, queryWrapper);
            model.addAttribute("weighings",page);
            model.addAttribute("weightstart",date1);
            model.addAttribute("weightend",date2);
            model.addAttribute("weilocation",weilocation);
            return "weight_data1";
        }else if(date1.equals("2001-06-19 16:16:12")&&date2.equals("4000-06-19 16:16:12")&&!weilocation.equals("-1")){
            if(loginUser.getUserlevel()==1){
                queryWrapper.eq("weicompany",loginUser.getUsercompany())
                            .eq("weilocation",weilocation);
            }else{
                queryWrapper.eq("weicompany",loginUser.getUsercompany())
                            .eq("weilocation",loginUser.getUserlocation());
            }
            Page<Weighing> page = weighingService.page(Tpage, queryWrapper);
            model.addAttribute("weighings",page);
            model.addAttribute("weightstart",date1);
            model.addAttribute("weightend",date2);
            model.addAttribute("weilocation",weilocation);
            return "weight_data1";
        }else{
            if(loginUser.getUserlevel()==1){
                queryWrapper.eq("weicompany",loginUser.getUsercompany());
            }else{
                queryWrapper.eq("weicompany",loginUser.getUsercompany())
                        .eq("weilocation",loginUser.getUserlocation());
            }
            Page<Weighing> page = weighingService.page(Tpage, queryWrapper);
            List<Weighing> records = page.getRecords();
            model.addAttribute("weighings",page);
            model.addAttribute("weightstart",date1);
            model.addAttribute("weightend",date2);
            model.addAttribute("weilocation",weilocation);
            return "weight_data";
        }
    }

    //查询异常数据
    @RequestMapping("/weightinglist/seaabdata")
    public String selectOneweightdata2(HttpSession session, @RequestParam(value ="startdata",required = false,defaultValue = "-1")Double data1, @RequestParam(value ="enddata",required = false,defaultValue = "999999999")Double data2, @RequestParam(value ="location",required = false,defaultValue = "-1")String location, @RequestParam(value = "pn",defaultValue = "1")Integer pn, Model model){
        User loginUser = (User) session.getAttribute("loginUser");
        QueryWrapper<Weighing> queryWrapper = new QueryWrapper<>();
        Page<Weighing> Tpage = new Page<>(pn,10);
        if(data1==-1&&data2==999999999&&location.equals("-1")||data1==-1&&data2!=999999999&&location.equals("-1")||data1!=-1&&data2==999999999&&location.equals("-1")){
            if(loginUser.getUserlevel()==1){
                queryWrapper.eq("weicompany",loginUser.getUsercompany());
                Page<Weighing> page = weighingService.page(Tpage, queryWrapper);
                model.addAttribute("weighings",page);
            }else{
                queryWrapper.eq("weilocation",loginUser.getUserlocation()).eq("weicompany",loginUser.getUsercompany());
                Page<Weighing> page = weighingService.page(Tpage, queryWrapper);
                model.addAttribute("weighings",page);
            }
            model.addAttribute("startdata",data1);
            model.addAttribute("enddata",data2);
            model.addAttribute("location",location);
            return "anomalies_alarms";
        }else{
            if(location.equals("-1")){
                if(loginUser.getUserlevel()==1){
                    queryWrapper.notBetween("weight",data1,data2).eq("weicompany",loginUser.getUsercompany());
                    Page<Weighing> page = weighingService.page(Tpage, queryWrapper);
                    model.addAttribute("weighings",page);
                }else{
                    queryWrapper.notBetween("weight",data1,data2).eq("weilocation",loginUser.getUserlocation()).eq("weicompany",loginUser.getUsercompany());
                    Page<Weighing> page = weighingService.page(Tpage, queryWrapper);
                    model.addAttribute("weighings",page);
                }
            }else{
                if(loginUser.getUserlevel()==1){
                    queryWrapper.notBetween("weight",data1,data2).eq("weicompany",loginUser.getUsercompany()).eq("weilocation",location);
                    Page<Weighing> page = weighingService.page(Tpage, queryWrapper);
                    model.addAttribute("weighings",page);
                }else{
                    queryWrapper.notBetween("weight",data1,data2).eq("weilocation",loginUser.getUserlocation()).eq("weicompany",loginUser.getUsercompany());
                    Page<Weighing> page = weighingService.page(Tpage, queryWrapper);
                    model.addAttribute("weighings",page);
                }
            }
            model.addAttribute("startdata",data1);
            model.addAttribute("enddata",data2);
            model.addAttribute("location",location);
            return "anomalies_alarms1";

        }
    }



    @InitBinder
    public void initBinder(ServletRequestDataBinder binder) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        binder.registerCustomEditor(Date.class, new CustomDateEditor(sdf, true));
    }
}

